package com.skeleton.server.guide.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.GuideCatgHotDto;
import com.skeleton.db.dto.GuideCatgUserDto;
import com.skeleton.db.dto.GuideItemUserDto;
import com.skeleton.db.dto.UserinfoDto;
import com.skeleton.server.guide.service.IGuideService;
import com.skeleton.server.guide.util.GuideConstants;
import com.skeleton.server.user.service.IUserService;
import com.skeleton.utils.SystemTools;

@Controller
@RequestMapping("guide/")
public class GuideControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IGuideService iGuideService;

	@RequestMapping("ajaxAddCatg")
	@ResponseBody
	public Map<String, Object> ajaxAddCatg(HttpServletRequest request, HttpSession session) {
		logger.info("-------user---ajaxAddCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String catg = request.getParameter("catg");

		UserinfoDto ud = (UserinfoDto) session.getAttribute(GuideConstants.LOGINUSER_SESSION_NAME);

		GuideCatgUserDto gcDto = new GuideCatgUserDto();
		gcDto.setCatgUserId(0);
		gcDto.setCatgUserTitle(catg);
		gcDto.setLoginname(ud.getUserLoginname());
		gcDto.setCatgUserSeq(0);

		ResultDto rd = iGuideService.doAddCatg(gcDto);
		if (rd.isSuccess()) {
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxLoadCatgList")
	@ResponseBody
	public Map<String, Object> ajaxLoadCatgList(HttpServletRequest request, HttpSession session) {
		logger.info("-------user---ajaxLoadCatgList-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		UserinfoDto ud = (UserinfoDto) session.getAttribute(GuideConstants.LOGINUSER_SESSION_NAME);

		ResultDto rd = iGuideService.doLoadCatgList(ud.getUserLoginname());
		if (rd.isSuccess()) {
			List<GuideCatgUserDto> gcdList = rd.getDtoList("GuideCatgUserDtoList");
			map.put("gcdList", gcdList);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxAddItem")
	@ResponseBody
	public Map<String, Object> ajaxAddItem(HttpServletRequest request) {
		logger.info("-------user---ajaxAddItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String title = request.getParameter("title");
		String url = request.getParameter("url");
		String catg = request.getParameter("catg");

		GuideItemUserDto giDto = new GuideItemUserDto();
		giDto.setItemUserId(SystemTools.getUUIDString());
		giDto.setCatgUserId(Integer.parseInt(catg));
		giDto.setItemUserTitle(title);
		giDto.setItemUserUrl(url);
		giDto.setItemUserIconUrl("test");
		giDto.setItemUserVisitCnt(0);
		giDto.setItemUserSeq(0);

		ResultDto rd = iGuideService.doAddGuideItem(giDto);
		if (rd.isSuccess()) {
			map.put("giDto", giDto);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxLoadGuide")
	@ResponseBody
	public Map<String, Object> ajaxLoadGuide(HttpServletRequest request, HttpSession session) {
		logger.info("-------user---ajaxLoadGuide-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		UserinfoDto ud = (UserinfoDto) session.getAttribute(GuideConstants.LOGINUSER_SESSION_NAME);

		ResultDto rd = iGuideService.getGuideByUser(ud.getUserLoginname());
		if (rd.isSuccess()) {
			List<GuideCatgUserDto> gcdList = rd.getDtoList("GuideCatgUserDtoList");
			map.put("gcdList", gcdList);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxDeleteItem")
	@ResponseBody
	public Map<String, Object> ajaxDeleteItem(HttpServletRequest request) {
		logger.info("-------user---ajaxDeleteItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String itemId = request.getParameter("itemId");

		GuideItemUserDto giDto = new GuideItemUserDto();
		giDto.setItemUserId(itemId);

		ResultDto rd = iGuideService.doDeleteItemById(giDto);
		if (rd.isSuccess()) {
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxAddVisitCnt")
	@ResponseBody
	public Map<String, Object> ajaxAddVisitCnt(HttpServletRequest request) {
		logger.info("-------user---ajaxAddVisitCnt-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String itemId = request.getParameter("item_id");

		GuideItemUserDto giDto = new GuideItemUserDto();
		giDto.setItemUserId(itemId);

		ResultDto rd = iGuideService.doAddVisitCnt(giDto);
		if (rd.isSuccess()) {
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxGetItemById")
	@ResponseBody
	public Map<String, Object> ajaxGetItemById(HttpServletRequest request) {
		logger.info("-------user---ajaxGetItemById-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String itemId = request.getParameter("item_id");

		GuideItemUserDto giDto = new GuideItemUserDto();
		giDto.setItemUserId(itemId);

		ResultDto rd = iGuideService.getItemById(giDto);
		if (rd.isSuccess()) {
			GuideItemUserDto gid = (GuideItemUserDto) rd.getFirstDto("GuideItemUserDto");
			map.put("gid", gid);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxModifyItem")
	@ResponseBody
	public Map<String, Object> ajaxModifyItem(HttpServletRequest request) {
		logger.info("-------user---ajaxModifyItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String itemId = request.getParameter("item_id");
		String title = request.getParameter("title");
		String url = request.getParameter("url");
		String catg = request.getParameter("catg");

		GuideItemUserDto giDto = new GuideItemUserDto();
		giDto.setItemUserId(itemId);
		giDto.setCatgUserId(Integer.parseInt(catg));
		giDto.setItemUserTitle(title);
		giDto.setItemUserUrl(url);
		giDto.setItemUserIconUrl("test");
		giDto.setItemUserVisitCnt(0);
		giDto.setItemUserSeq(0);

		ResultDto rd = iGuideService.doModifyItemById(giDto);
		if (rd.isSuccess()) {
			map.put("giDto", giDto);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxGetCatgById")
	@ResponseBody
	public Map<String, Object> ajaxGetCatgById(HttpServletRequest request) {
		logger.info("-------user---ajaxGetCatgById-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String catgId = request.getParameter("catg_id");

		GuideCatgUserDto gcDto = new GuideCatgUserDto();
		gcDto.setCatgUserId(Integer.parseInt(catgId));

		ResultDto rd = iGuideService.getCatgById(gcDto);
		if (rd.isSuccess()) {
			GuideCatgUserDto gcd = (GuideCatgUserDto) rd.getFirstDto("GuideCatgUserDto");
			map.put("gcd", gcd);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxModifyCatg")
	@ResponseBody
	public Map<String, Object> ajaxModifyCatg(HttpServletRequest request) {
		logger.info("-------user---ajaxModifyCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String catgId = request.getParameter("catg_id");
		String title = request.getParameter("title");

		GuideCatgUserDto gcDto = new GuideCatgUserDto();
		gcDto.setCatgUserId(Integer.parseInt(catgId));
		gcDto.setCatgUserTitle(title);

		ResultDto rd = iGuideService.doModifyCatgById(gcDto);
		if (rd.isSuccess()) {
			map.put("gcDto", gcDto);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxDeleteCatg")
	@ResponseBody
	public Map<String, Object> ajaxDeleteCatg(HttpServletRequest request) {
		logger.info("-------user---ajaxDeleteCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String catgId = request.getParameter("catg_id");

		GuideCatgUserDto gcDto = new GuideCatgUserDto();
		gcDto.setCatgUserId(Integer.parseInt(catgId));

		ResultDto rd = iGuideService.doDeleteCatgById(gcDto);
		if (rd.isSuccess()) {
			map.put("catgId", catgId);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxLoadHotGuide")
	@ResponseBody
	public Map<String, Object> ajaxLoadHotGuide(HttpServletRequest request) {
		logger.info("-------user---ajaxLoadHotGuide-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		ResultDto rd = iGuideService.getHotGuide();
		if (rd.isSuccess()) {
			List<GuideCatgHotDto> gchdList = rd.getDtoList("GuideCatgHotDtoList");
			map.put("gchdList", gchdList);
			map.put("result", GuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", GuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
